package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.alipay.sdk.util.l;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.Observable;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.SecondHandMainShowContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.SecondHandCategoryBean;
import com.kmproject.haifu.mvp.model.entity.beans.SecondHandInfoBean;
import com.kmproject.haifu.mvp.model.entity.resbean.SecondHandQueryParmar;
import com.kmproject.haifu.mvp.model.entity.resbean.SecondHandSaveRequest;

import java.util.List;
import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/29/2019 19:26
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class SecondHandMainShowPresenter extends BasePresenter<SecondHandMainShowContract.Model, SecondHandMainShowContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public SecondHandMainShowPresenter(SecondHandMainShowContract.Model model, SecondHandMainShowContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 查询关注的二手店铺
     * @param secondHandQueryParmar
     */
    public void myAttention(SecondHandQueryParmar secondHandQueryParmar){
      mModel.myAttention(AppUtil.entityToMap(secondHandQueryParmar)).compose(RxUtils.applySchedulers(mRootView))
      .subscribe(new ErrorHandleSubscriber<ResultBean<List<SecondHandInfoBean>>>(mErrorHandler) {
          @Override
          public void onNext(ResultBean<List<SecondHandInfoBean>> listResultBean) {
              if (AppUtil.isSuccess(listResultBean))
              {
                  mRootView.listSecondHandSuccessa(Integer.parseInt(listResultBean.getExtra().getPage().getCurrent()),listResultBean.getData());
              }else {
                  mRootView.showMessage(listResultBean.getMessage());
              }
          }
      });
    }
    /**
     * 查询列表
     * @param secondHandQueryParmar
     */
    public void  listSecondHand(SecondHandQueryParmar secondHandQueryParmar) {
        mModel.listSecondHand(AppUtil.entityToMap(secondHandQueryParmar)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<SecondHandInfoBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<SecondHandInfoBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean))
                        {
                            mRootView.listSecondHandSuccessa(Integer.parseInt(listResultBean.getExtra().getPage().getCurrent()),listResultBean.getData());
                        }else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 获取分类列表
     * @param categoryId
     * @param name
     * @param parentId
     */
    public void  listSecondCategory(String categoryId, String name, String parentId) {
        mModel.listSecondCategory(categoryId,  name,  parentId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<SecondHandCategoryBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<SecondHandCategoryBean>> listResultBean) {
                       if (AppUtil.isSuccess(listResultBean))
                       {
                           mRootView.listSecondCategorySuccess(listResultBean.getData());

                       }    else {
                           mRootView.showMessage(listResultBean.getMessage());
                       }
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
